<template>
  <a-layout-header class="header">
    <div class="logo">wiki知识库</div>
    <a-menu
        theme="dark"
        mode="horizontal"
        :style="{ lineHeight: '64px',backgroundColor: 'transparent', color: 'white' }"
    >
      <a-menu-item key="/">
        <router-link to="/">
          首页
        </router-link>
      </a-menu-item>
      <a-menu-item key="/admin/user" v-if="user.id">
        <router-link to="/admin/user">
          用户管理
        </router-link>
      </a-menu-item>
      <a-menu-item key="/admin/ebook" v-if="user.id">
        <router-link to="/admin/ebook">
          电子书管理
        </router-link>
      </a-menu-item>
      <a-menu-item key="/admin/category" v-if="user.id">
        <router-link to="/admin/category">
          分类管理
        </router-link>
      </a-menu-item>
      <a-menu-item key="/about">
        <router-link to="/about">
          关于我们
        </router-link>
      </a-menu-item>
      <div id="login-menu-div">
        <a class="login-menu" v-show="user.name" @click="logout">
          <span>退出登录</span>
        </a>
        <a class="login-menu" v-show="user.name">
          <span>您好：{{user.name}}</span>
        </a>
        <a class="login-menu" v-show="!user.name" @click="showLoginModal">
          <span>登录</span>
        </a>
      </div>

    </a-menu>

    <a-modal
        title="登录"
        v-model:visible="loginModalVisible"
        :confirm-loading="loginModalLoading"
        @ok="login"
    >
      <a-form :model="loginUser" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
        <a-form-item label="登录名">
          <a-input v-model:value="loginUser.loginName"/>
        </a-form-item>
        <a-form-item label="密码">
          <a-input v-model:value="loginUser.password" type="password"/>
        </a-form-item>
      </a-form>
    </a-modal>

  </a-layout-header>
</template>

<script lang="ts">
import {defineComponent, ref, computed, createVNode} from 'vue';
import axios from 'axios';
import {message, Modal} from 'ant-design-vue';
import store from "@/store";
import {ExclamationCircleOutlined} from "@ant-design/icons-vue";
import router from "@/router";

declare let hexMd5: any;
declare let KEY: any;

export default defineComponent({
  name: 'the-header',

  setup() {

    // 用来存储用户信息
    const user = computed(() => store.state.user);

    // 用来登录
    const loginUser = ref({
      loginName: "admin",
      password: "123456"
    });
    const loginModalVisible = ref(false);
    const loginModalLoading = ref(false);
    const showLoginModal = () => {
      loginModalVisible.value = true;
    };

    // 登录
    const login = () => {
      console.log("开始登录");
      loginModalLoading.value = true;
      loginUser.value.password = hexMd5(loginUser.value.password + KEY);
      axios.post('/user/login', loginUser.value).then((response) => {
        loginModalLoading.value = false;
        const data = response.data;
        if (data.success) {
          loginModalVisible.value = false;
          store.commit("setUser", data.content);// 将用户信息存储到vuex中
          console.log("登录成功，用户信息：", data.content);
          message.success("登录成功！");
        } else {
          message.error(data.message);
        }
      });
    };

    // 退出登录
    const logout = () => {
      console.log("退出登录开始");
      Modal.confirm({
        title: () => '退出登录',
        icon: () => createVNode(ExclamationCircleOutlined),
        content: () => '确定退出登录吗?',
        onOk() {
          axios.get('/user/logout/'+user.value.token).then((response) => {
            const data = response.data;
            if (data.success) {
              store.commit("setUser", {});// 将用户信息存储到vuex中
              console.log("退出登录成功");
              message.success("退出登录成功！");
              // 跳转到首页
              router.push("/");
            } else {
              message.error(data.message);
            }
          });
        },
        // eslint-disable-next-line @typescript-eslint/no-empty-function
        onCancel() {},
      });
    };

    return {
      loginModalVisible,
      loginModalLoading,
      showLoginModal,
      loginUser,
      user,
      login,
      logout
    }
  }

});
</script>

<style>
.logo {
  width: 120px;
  height: 31px;
  /*background: rgba(255, 255, 255, 0.2);*/
  /*margin: 16px 28px 16px 0;*/
  float: left;
  color: white;
  font-size: 18px;
}
.login-menu {
  float: right;
  color: white;
  padding-left: 10px;
}
#login-menu-div {
  width: 300px;
  position: fixed;
  margin: 0px 1000px;
}
</style>